package org.hxyjs.service.impl;


import org.hxyjs.dao.SchoolDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.School;
import org.hxyjs.entity.School;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.SchoolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("SchoolService")
public class SchoolServiceImpl implements SchoolService {
    @Resource
    SchoolDao schoolDao;
    @Override
    public DTO<School> getAllSchools()  throws Exception{
        List<School> schools = schoolDao.getAllSchools();
        MessageEnum messageEnum  = Optional.ofNullable(schools).isPresent()?MessageEnum.GETSCHOOLSUCCESS: MessageEnum.GETSCHOOLFAIL;
        return new DTO<School>(messageEnum.getCode(),messageEnum.getMessage(),schools);
    }

    @Override
    public DTO<School> deleteSchoolBySno(String sno)  throws Exception{
        Integer result = schoolDao.deleteSchoolBySno(sno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETESCHOOLSUCCESS:MessageEnum.DELETESCHOOLFAIL;
        return new DTO<School>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<School> updateSchoolBySno(School school)  throws Exception{
        Integer result = schoolDao.updateSchoolBySno(school);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATESCHOOLSUCCESS:MessageEnum.UPDATESCHOOLFAIL;
        return new DTO<School>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<School> addSchool(School school) throws Exception {
        Integer result = schoolDao.addSchool(school);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDSCHOOLSUCCESS:MessageEnum.ADDSCHOOLFAIL;
        return new DTO<School>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<School> getSchoolByPage(PageInfo pageInfo ) throws Exception {
        List<School> schools = schoolDao.getSchoolByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(schools).isPresent()?MessageEnum.GETSCHOOLSUCCESS: MessageEnum.GETSCHOOLFAIL;
        return new DTO<School>(messageEnum.getCode(),messageEnum.getMessage(),schools,pageInfo);
    }
}
